/**
 * Copyright 2013 Mr.Hai Mobile Developer. All rights reserved.
 * HAI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hector.meinewerk.model;

import java.util.ArrayList;
import java.util.Vector;

import android.graphics.Bitmap;

import com.hector.meinewerk.common.ActionEvent;
import com.hector.meinewerk.common.GlobalInfo;
import com.hector.meinewerk.common.ServerPath;
import com.hector.meinewerk.constants.Constants;
import com.hector.meinewerk.lib.network.http.HTTPListenner;
import com.hector.meinewerk.lib.network.http.HTTPMessage;
import com.hector.meinewerk.lib.network.http.HTTPMultiPartRequest;
import com.hector.meinewerk.lib.network.http.HTTPRequest;
import com.hector.meinewerk.lib.network.http.HTTPResponse;
import com.hector.meinewerk.lib.network.http.HttpAsyncTask;
import com.hector.meinewerk.lib.network.http.MultiPartInputStream;
import com.hector.meinewerk.lib.network.http.NetworkUtil;

/**
 * 
 * abstract model service
 * 
 * @author: HaiTC3
 * @version: 1.1
 * @since: 1.0 | Mar 7, 2013
 */
public abstract class AbstractModelService implements HTTPListenner {

	public void onReceiveError(HTTPResponse response) {
		// TODO Auto-generated method stub

	}

	public void onReceiveData(HTTPMessage mes) {
		// TODO Auto-generated method stub

	}

	/**
	 * Request text
	 * 
	 * @author: HaiTC
	 * @param method
	 * @param data
	 * @param actionEvent
	 * @return: void
	 * @throws:
	 */
	protected HTTPRequest sendHttpRequest(String method, Vector data,
			ActionEvent actionEvent) {
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append(NetworkUtil.getJSONString(method, data));
		HTTPRequest re = new HTTPRequest();
		re.setUrl(ServerPath.SERVER_PATH);
		re.setAction(actionEvent.action);
		re.setContentType(HTTPMessage.CONTENT_JSON);
		re.setMethod(Constants.HTTPCONNECTION_POST);
		re.setDataText(strBuffer.toString());
		re.setObserver(this);
		re.setUserData(actionEvent);
		new HttpAsyncTask(re).execute();
		return re;
	}

	/**
	 * Request text
	 * 
	 * @author: HaiTC
	 * @param method
	 * @param data
	 * @param actionEvent
	 * @return: void
	 * @throws:
	 */
	protected HTTPRequest sendHttpRequest(String methodName,
			String requestMethod, ArrayList<String> params, ActionEvent actionEvent,
			int timeout) {
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append(NetworkUtil.getJSONStringToParam(params));
		HTTPRequest re = new HTTPRequest();
		re.setUrl(ServerPath.SERVER_PATH);
		re.setAction(actionEvent.action);
		re.setMethodName(methodName);
		re.setContentType(HTTPMessage.CONTENT_JSON);
		re.setMethod(requestMethod);
		re.setDataText(strBuffer.toString());
		re.setObserver(this);
		re.setUserData(actionEvent);
		new HttpAsyncTask(re, timeout).execute();
		return re;
	}

	/**
	 * Request text
	 * 
	 * @author: HaiTC
	 * @param method
	 * @param data
	 * @param actionEvent
	 * @return: void
	 * @throws:
	 */
	protected HTTPRequest sendRSAHttpRequest(String method, Vector data,
			ActionEvent actionEvent, boolean usingAES, int timeout) {
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append(NetworkUtil.getJSONStringRSA(method, data,
				actionEvent, usingAES));
		HTTPRequest re = new HTTPRequest();
		re.setUrl(ServerPath.SERVER_PATH);
		re.setAction(actionEvent.action);
		re.setContentType(HTTPMessage.CONTENT_JSON);
		re.setMethod(Constants.HTTPCONNECTION_POST);
		re.setDataText(strBuffer.toString());
		re.setObserver(this);
		re.setUserData(actionEvent);
		new HttpAsyncTask(re, timeout).execute();
		return re;
	}

	/**
	 * Request multipart
	 * 
	 * @author: HaiTC
	 * @param method
	 * @param data
	 * @param imgPath
	 * @param actionEvent
	 * @param fileName
	 * @param fileField
	 * @param fileType
	 * @return: void
	 * @throws Throwable
	 * @throws:
	 */
	@SuppressWarnings("rawtypes")
	protected HTTPRequest httpMultiPartRequest(String method, Vector data,
			String imgPath, ActionEvent actionEvent, String fileName,
			String fileField, String fileType) throws Throwable {
		Bitmap bmp = GlobalInfo.getInstance().getBitmapData();
		HTTPRequest re = new HTTPMultiPartRequest();
		re.setContentType(HTTPMessage.MULTIPART_JSON);
		re.setDataTypeSend(HTTPRequest.CONTENT_TYPE_BINARY);
		MultiPartInputStream multiPartStream;
		String dataText = NetworkUtil.getJSONString(method, data);
		if (bmp != null) {
			multiPartStream = new MultiPartInputStream(dataText, bmp, fileName,
					fileField, fileType);

		} else {
			multiPartStream = new MultiPartInputStream(dataText, imgPath,
					fileName, fileField, fileType);
		}
		((HTTPMultiPartRequest) re).setMultipartStream(multiPartStream);
		((HTTPMultiPartRequest) re).setDataText(dataText);
		re.setUrl(ServerPath.SERVER_PATH);
		re.setAction(actionEvent.action);
		re.setMethod("POST");
		re.setObserver(this);
		re.setUserData(actionEvent);

		new HttpAsyncTask(re).execute();
		return re;
	}

	/**
	 * Request multipart voi anh duoc decode tu link image
	 * 
	 * @author: HaiTC
	 * @param method
	 * @param data
	 * @param imgPath
	 * @param actionEvent
	 * @param fileName
	 * @param fileField
	 * @param fileType
	 * @param isUsedDecodeBitmapFromImgPath
	 * @return
	 * @return: HTTPRequest
	 * @throws Throwable
	 * @throws:
	 */
	@SuppressWarnings("rawtypes")
	protected HTTPRequest httpMultiPartRequestWithDeoceImageFromImagePath(
			String method, Vector data, String imgPath,
			ActionEvent actionEvent, String fileName, String fileField,
			String fileType) throws Throwable {
		HTTPRequest re = new HTTPMultiPartRequest();
		re.setContentType(HTTPMessage.MULTIPART_JSON);
		re.setDataTypeSend(HTTPRequest.CONTENT_TYPE_BINARY);
		MultiPartInputStream multiPartStream;
		String dataText = NetworkUtil.getJSONString(method, data);
		multiPartStream = new MultiPartInputStream(dataText, imgPath, fileName,
				fileField, fileType);
		((HTTPMultiPartRequest) re).setMultipartStream(multiPartStream);
		((HTTPMultiPartRequest) re).setDataText(dataText);
		re.setUrl(ServerPath.SERVER_PATH);
		re.setAction(actionEvent.action);
		re.setMethod("POST");
		re.setObserver(this);
		re.setUserData(actionEvent);

		new HttpAsyncTask(re).execute();
		return re;
	}

}
